package com.lagou.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class TokenFilter implements GlobalFilter, Ordered {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("/code/create/996882153@qq.com".startsWith("/code"));
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        if (!request.getPath().toString().startsWith("/user/getEmailByToken")
        &&!request.getPath().toString().startsWith("/code")
                &&!request.getPath().toString().startsWith("/user/register")
                &&!request.getPath().toString().startsWith("/user/login")){
            MultiValueMap<String, HttpCookie> cookies = request.getCookies();
            String token = null;
            if (cookies!=null && cookies.size()>0){
                for (String key : cookies.keySet()) {
                    if ("user-cookie".equals(key)){
                        HttpCookie httpCookie = cookies.get(key).get(0);
                        token = httpCookie.getValue();
                    }
                }
                if (token!=null){
                    /*ResponseData responseData = userClient.getEmailByToken(token);
                    if (responseData.getCode() == 200){
                        //跳转欢迎页面
                        ResponseData data = ResponseData.to302("跳转欢迎页面","/static/html/welcome.html");
                        String json = JSON.toJSONString(data);
                        byte[] bits = json.getBytes(StandardCharsets.UTF_8);
                        DataBuffer buffer = response.bufferFactory().wrap(bits);
                        response.setStatusCode(HttpStatus.OK);
                        //指定编码，否则在浏览器中会中文乱码
                        response.getHeaders().add("Content-Type", "text/plain;charset=UTF-8");
                        return response.writeWith(Mono.just(buffer));
                    }*/
                    String url = "http://localhost/static/html/welcome.html";
                    //303状态码表示由于请求对应的资源存在着另一个URI，应使用GET方法定向获取请求的资源
                    response.setStatusCode(HttpStatus.SEE_OTHER);
                    response.getHeaders().set(HttpHeaders.LOCATION, url);
                    return response.setComplete();
                }
            }
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return -1;
    }
}
